<?php

namespace plugin\admin\app\service\v1;

use plugin\admin\app\common\Util;
use plugin\admin\app\constant\v1\CommonConstant;
use support\exception\BusinessException;

/**
 * 服务器服务类
 **/
class ServerService
{

    /**
     * 重启
     *
     * @param array $ids
     **/
    public static function rebootServer($ids)
    {
        $db = Util::db();
        $list = $db->table('wa_v1_server')
            ->whereIn('id', $ids)
            ->where('deleted', CommonConstant::DELETE_0)
            ->select('id', 'name', 'ip', 'port', 'key')
            ->get();
        if ($list->isEmpty()) {
            throw new BusinessException('服务器不存在或已删除！');
        }

        $errorInfo = '';
        foreach ($list as $item) {
            $url = 'http://' . $item->ip . ':' . $item->port . '/api/panel/RebootServer';
            $result = Util::rebootServerApi($url,['key'=>$item->key]);
            if ($result['code'] != 200) {
                $errorInfo .= '服务器：' . $item->name . ' ' . $result['msg'] . ' ';
            }
        }
        if ($errorInfo) {
            throw new BusinessException($errorInfo);
        }
        return true;
    }

}
